package com.qingzhuge.common;

import com.qingzhuge.common.utils.IDCardUtil;
import com.qingzhuge.dto.response.BaseResponseDto;
import com.qingzhuge.exception.MobileException;
import com.qingzhuge.exception.NumberException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : zero.xiao
 * @description :
 * @modified :
 */
@Slf4j
public final class Assert {
    private static String getMessage(String key, Object... args) {
        return Resources.getMessage(key, args);
    }

    /**  */
    public static boolean isTrue(boolean expression, String key) {
        if (!expression) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return true;
    }

    /**  */
    public static <T> T isNull(T object, String key) {
        if (Objects.nonNull(object)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return null;
    }

    /**
     * (key_IS_NULL)
     */
    public static <T> T notNull(T object, String key, Object... args) {
        if (Objects.isNull(object)) {
            throw new IllegalArgumentException(getMessage(key + "_IS_NULL", args));
        }
        return object;
    }

    /**
     * 判断某字符串不为空
     */
    public static String hasLength(String text, String key) {
        if (StringUtils.isEmpty(text)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return text;
    }

    /**
     * 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
     */
    public static String hasText(String text, String key) {
        if (StringUtils.isBlank(text)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return text;
    }

    /**
     * 字符textToSearch 中是否包含 substring
     */
    public static void doesNotContain(String textToSearch, String substring, String key) {
        if (StringUtils.isNotBlank(textToSearch) && StringUtils.isNotBlank(substring)
                && textToSearch.contains(substring)) {
            throw new IllegalArgumentException(getMessage(key));
        }
    }

    /**
     * 是否为空数组
     */
    public static <T> T[] notEmpty(T[] array, String key, Object... args) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(getMessage(key + "_IS_EMPTY", args));
        }
        return array;
    }

    /**
     * 数组成员中是否有null值
     */
    public static <T> T[] noNullElements(T[] array, String key) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new IllegalArgumentException(getMessage(key));
                }
            }
        }
        return array;
    }

    /**
     * 集合不为null，长度不为0
     */
    public static <T> Collection<T> notEmpty(Collection<T> collection, String key) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return collection;
    }

    /**
     * 集合不为null，长度不为0
     */
    public static <K, V> Map<K, V> notEmpty(Map<K, V> map, String key) {
        if (CollectionUtils.isEmpty(map)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return map;
    }

    /**  */
    public static <T> Class<T> isInstanceOf(Class<T> type, Object obj, String key) {
        if (!notNull(type, key).isInstance(obj)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return type;
    }

    /**  */
    public static void isAssignable(Class<?> superType, Class<?> subType, String key) {
        if (subType == null || !notNull(superType, key).isAssignableFrom(subType)) {
            throw new IllegalArgumentException(getMessage(key));
        }
    }

    /**
     * 空字符或NULL
     */
    public static String isBlank(String text, String key) {
        if (StringUtils.isNotBlank(text)) {
            throw new IllegalArgumentException(getMessage(key));
        }
        return text;
    }

    /**
     * 非空字符串(key_IS_NULL)
     */
    public static String isNotBlank(String text, String key) {
        if (StringUtils.isBlank(text)) {
            throw new IllegalArgumentException(getMessage(key + "_IS_NULL"));
        }
        return text;
    }

    /**
     * 允许最小值
     */
    public static Integer min(Integer value, Integer min, String key) {
        if (notNull(value, key) < min) {
            throw new IllegalArgumentException(getMessage(key + "_MIN", min));
        }
        return value;
    }

    /**
     * 允许最大值
     */
    public static Integer max(Integer value, Integer max, String key) {
        if (notNull(value, key) > max) {
            throw new IllegalArgumentException(getMessage(key + "_MAX", max));
        }
        return value;
    }

    /**
     * 允许值范围
     */
    public static Integer range(Integer value, Integer min, Integer max, String key) {
        min(value, min, key);
        max(value, max, key);
        return value;
    }

    /**
     * 允许最小值
     */
    public static Float min(Float value, Float min, String key) {
        if (notNull(value, key) < min) {
            throw new IllegalArgumentException(getMessage(key + "_MIN", min));
        }
        return value;
    }

    /**
     * 允许最大值
     */
    public static Float max(Float value, Float max, String key) {
        if (notNull(value, key) > max) {
            throw new IllegalArgumentException(getMessage(key + "_MAX", max));
        }
        return value;
    }

    /**
     * 允许值范围
     */
    public static Float range(Float value, Float min, Float max, String key) {
        min(value, min, key);
        max(value, max, key);
        return value;
    }

    /**
     * 允许最小值
     */
    public static Double min(Double value, Double min, String key) {
        if (notNull(value, key) < min) {
            throw new IllegalArgumentException(getMessage(key + "_MIN", min));
        }
        return value;
    }

    /**
     * 允许最大值
     */
    public static Double max(Double value, Double max, String key) {
        if (notNull(value, key) > max) {
            throw new IllegalArgumentException(getMessage(key + "_MAX", max));
        }
        return value;
    }

    /**
     * 允许值范围
     */
    public static Double range(Double value, Double min, Double max, String key) {
        min(value, min, key);
        max(value, max, key);
        return value;
    }

    /**
     * 字符长度范围(key_LENGTH)
     */
    public static String length(String text, Integer min, Integer max, String key) {
        if (min != null && notNull(text, key).length() < min) {
            throw new IllegalArgumentException(getMessage(key + "_LENGTH", min, max));
        }
        if (max != null && notNull(text, key).length() > max) {
            throw new IllegalArgumentException(getMessage(key + "_LENGTH", min, max));
        }
        return text;
    }

    /**
     * 未来某一天
     */
    public static Date future(Date date, String key) {
        if (date != null && date.compareTo(new Date()) <= 0) {
            throw new IllegalArgumentException(getMessage(key + "_NOT_FUTURE"));
        }
        return date;
    }

    /**
     * 身份证
     */
    public static String idNumber(String text) {
        if (!IDCardUtil.isIdentity(text)) {
            throw new IllegalArgumentException(getMessage("IDCARD_ILLEGAL"));
        }
        return text;
    }

    /**
     * 邮箱
     */
    public static String email(String text) {
        String regex = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        pattern(text, regex, true, "EMAIL");
        return text;
    }

    /**
     * 手机号
     */
    public static String mobile(String text) {
        String regex = "((^(13|14|15|16|17|18|19)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        try {
            pattern(text, regex, true, "MOBILE");
            return text;
        } catch (Exception e) {
            throw new MobileException();
        }
    }

    public static boolean validationMobile(String mobile, BaseResponseDto response) {
        try {
            Assert.mobile(mobile);
        } catch (IllegalArgumentException e) {
            response.setResCode(HttpCode.MOBILE_ERROR.value());
            response.setResMessage(e.getMessage());
            log.error("{}:{}", e.getMessage(), mobile);
            return true;
        }
        return false;
    }

    public static boolean number(String text){
        String regex = "[0-9]*";
        try {
            pattern(text,regex,true,"NUMBER");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new NumberException(e.getMessage());
        }
    }

    /**
     * 正则表达式
     */
    public static boolean pattern(String text, String regex, boolean flag, String key) {
        boolean result;
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            result = matcher.matches();
        } catch (Exception e) {
            result = false;
        }
        if (result != flag) {
            throw new IllegalArgumentException(getMessage(key + "_ILLEGAL"));
        }
        return true;
    }
}
